package com.unittec.zk.provider.work.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.provider.work.entity.MesCollection;
import com.unittec.zk.provider.work.service.MesCollectionService;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import com.unittec.zk.sdk.work.request.Req202008;
import com.unittec.zk.sdk.work.request.Req202009;
import com.unittec.zk.sdk.work.request.Req202011;
import com.unittec.zk.sdk.work.request.Req202010;
import com.unittec.zk.sdk.work.response.Res202000;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "短信管理")
public class MesController {
    
    @Resource
    private Log log;
    
    @Resource
    private MesCollectionService service;
    
    //短信-增/改
    @RequestMapping(value = "/202008", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "短信-增/改", notes = "短信-增/改")
    public Result<Res202000> saveOrUpdateMes(@RequestBody GatewayRequest<Req202008> req) {
        log.debug(req, "saveOrUpdateMes {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.saveOrUpdateMes(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202008", JSON.toJSONString(req), "短信-增/改", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "saveOrUpdateMes {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202008", JSON.toJSONString(req), "短信-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202008", JSON.toJSONString(req), "短信-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //短信分页
    @RequestMapping(value = "/202009", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "短信分页", notes = "短信分页")
    public Result<Res202000> getMesPage(@RequestBody GatewayRequest<Req202009> req) {
        log.debug(req, "getMesPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.getMesPage(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202009", JSON.toJSONString(req), "短信分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getMesPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202009", JSON.toJSONString(req), "短信分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202009", JSON.toJSONString(req), "短信分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    //短信-删
    @RequestMapping(value = "/202011", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "短信-删", notes = "短信-删")
    public Result<Res202000> mesInfoDelbyIds(@RequestBody GatewayRequest<Req202011> req) {
        log.debug(req, "mesInfoDelbyIds {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.mesInfoDelbyIds(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202011", JSON.toJSONString(req), "短信信息-删", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "mesInfoDelbyIds {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202011", JSON.toJSONString(req), "短信信息-删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202011", JSON.toJSONString(req), "短信信息-删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //发送详情

    @RequestMapping(value = "/202010", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "发送详情", notes = "发送详情")
    public Result<Res202000> sendDetail(@RequestBody GatewayRequest<Req202010> req){
        log.debug(req, "sendDetail {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res202000 res202000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = service.sendDetail(req.getContent());
            res202000 = new Res202000();
            res202000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("202010", JSON.toJSONString(req), "发送详情", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "sendDetail {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("202010", JSON.toJSONString(req), "发送详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res202000);
        } else {
            log.addOperateLog("202010", JSON.toJSONString(req), "发送详情", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

}
